<?php
/*
	排队
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
if($_RQ['store_id']){
	$store_id = intval($_RQ['store_id']);
	$this->session('store_id',$store_id);
}
if(!$store_id){
	$store_id=$this->session('store_id');
}
if(!$store_id){
	exi('请先选择门店！','error','jy_meye/index/');
}
if($_RQ['op']=='main'){
	$return['params']=array();
	$return['member']=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	if(!$return['member']){
		exi('找不到会员信息！','error');
	}
	if($return['member']['groupid']){
		$return['member']['group']=pdo_getcolumn('core_member_groups',array('id'=>$return['member']['groupid'],'uniacid'=>$_SESSION['uniacid']),'title');
	}else{
		$return['member']['group']='普通会员';
	}
	//查询会员取号信息
	$myquhao = pdo_get('jy_meye_queue_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>0));
	//是否展示取号表单
	if($myquhao['queue_id']){
		$show = 1;
	    //取号后  查询会员前面排队人数
		$myqueue = pdo_get('jy_meye_queue',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'id'=>$myquhao['queue_id']));
		$myquhao['pre'] = $myqueue['pre'];
		$myquhao['queuename'] = $myqueue['title'];
        $queue_num = pdo_count('jy_meye_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>0,'queue_id'=>$myquhao['queue_id'],'createtime <' =>$myquhao['createtime']));
		$nowmember = pdo_getall('jy_meye_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>1),'','',' id desc');
	}else{
		$show = 0;
	}
	$queues = pdo_getall('jy_meye_queue',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>1),'','',' id desc');
	//查询门店设置
    $setting=$this->store_setting($store_id,'queue');
	$return['show'] = $show;
	$return['nowmember'] = $nowmember[0];
	$return['myquhao'] = $myquhao;
	$return['queues'] = $queues;
	$return['nowhao'] = $quhao['id']-$queue_num;
	$return['queue_num'] = $queue_num;
	$return['setting'] = $setting;
	set_html('title','排号管理');
	exi($return);
}
if($_RQ['op']=='set'){
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$myquhao = pdo_get('jy_meye_queue_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>0,'createtime >'=>$daytime));
	if(!empty($myquhao)){
		exi('您已经取号了！','error');
	}
    if($_RQ['postdata']){
		$postdata=get_postdata();
		$queue = pdo_getall('jy_meye_queue',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>1,'limit_num >='=>$postdata['num']));
		if(empty($queue)){
			exi('没有找到适合的餐桌！','error');
		}else{
			$limit_num =0;
			foreach($queue as $item){
				if($limit_num<$item['limit_num']){
					$queue_id = $item['id'];
					$queue_pre = $item['pre'];
				}
				$limit_num = $item['limit_num'];
			}
		}
		$number = pdo_count('jy_meye_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'queue_id'=>$queue_id,'createtime >'=>$daytime));
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'queue_id'=>$queue_id,
			'num'=>intval($postdata['num']),
			'mid'=>$_SESSION['uid'],
			'number'=>$queue_pre.sprintf("%05d", $number+1),
            'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_meye_queue_member',$data);
		$id = pdo_insertid();
		//发信息
		$this->notice('queue_update',$_SESSION['uid'],$store_id,$id);
		exi('取号成功');
	}else{
		$return=array();
		exi($return);
	}
}